<html>
<head><meta charset="utf-8"><title>using enum for members of rustc_target::spec::Target ? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/using.20enum.20for.20members.20of.20rustc_target.3A.3Aspec.3A.3ATarget.20.3F.html">using enum for members of rustc_target::spec::Target ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212173694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/using%20enum%20for%20members%20of%20rustc_target%3A%3Aspec%3A%3ATarget%20%3F/near/212173694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/using.20enum.20for.20members.20of.20rustc_target.3A.3Aspec.3A.3ATarget.20.3F.html#212173694">(Oct 03 2020 at 13:58)</a>:</h4>
<p>Why does most of rustc_target::spec::Target members use string as their type ?<br>
For example: <code>target_endian</code> could be an enum Endian .</p>



<a name="213479005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/using%20enum%20for%20members%20of%20rustc_target%3A%3Aspec%3A%3ATarget%20%3F/near/213479005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> est31 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/using.20enum.20for.20members.20of.20rustc_target.3A.3Aspec.3A.3ATarget.20.3F.html#213479005">(Oct 15 2020 at 19:53)</a>:</h4>
<p>@luzato I think it was chosen because it's easiest to map those values between json and Rust. Also because of configurability. The point of json is to enable you to avoid having to modify compiler sources to add a new target, at least in cases where this is possible. Originally the target operating system <a href="https://github.com/rust-lang/rust/blob/ff00edb7e1fd669b7a333e0b3842f3751ee6d573/src/rustc/driver/session.rs#L13">was an enum</a> for example, so if you wanted to add a new OS, you'd have to modify compiler sources. Strings give you thus the maximum freedom.</p>
<p>However, the current implementation is too extremely string based. The rule of thumb should be: is it possible for a target to add a new value not seen in the builtin targets without having to modify rustc to do something special about the new value? Endianness is a clear example. A target can decide that its values are two dimensional quadratic matrices and the bytes are stored in cantor pairing fashion, which would be something other than big or little endian for the endinaness, but it would obviously require deep changes of rustc and llvm :). Operating system vendors are another example. Maybe amazon wants to add unknown-linux-amazon or something, but doesn't need any changes of the compiler code itself to handle it. If there is ambiguity, one should err on the side of strings, but not as much as the current implementation :).</p>



<a name="213520142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/using%20enum%20for%20members%20of%20rustc_target%3A%3Aspec%3A%3ATarget%20%3F/near/213520142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/using.20enum.20for.20members.20of.20rustc_target.3A.3Aspec.3A.3ATarget.20.3F.html#213520142">(Oct 16 2020 at 05:23)</a>:</h4>
<p>I understand the flexibility of using string.<br>
rust and LLVM only accepts and processes two endianness value: little and big.<br>
So even if users add a json target with endian different than those, they still have to change rustc and llvm<br>
to process that.</p>



<a name="213520204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/using%20enum%20for%20members%20of%20rustc_target%3A%3Aspec%3A%3ATarget%20%3F/near/213520204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/using.20enum.20for.20members.20of.20rustc_target.3A.3Aspec.3A.3ATarget.20.3F.html#213520204">(Oct 16 2020 at 05:24)</a>:</h4>
<p>another possibility is to <code>impl FromStr for Endian</code>, then use <code>"little".parse().unwrap()</code>.</p>



<a name="213520278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/using%20enum%20for%20members%20of%20rustc_target%3A%3Aspec%3A%3ATarget%20%3F/near/213520278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/using.20enum.20for.20members.20of.20rustc_target.3A.3Aspec.3A.3ATarget.20.3F.html#213520278">(Oct 16 2020 at 05:26)</a>:</h4>
<p>also, for OS, I think we could use <code>Other(String)</code> variant for unknown OSes.<br>
the json processing part could change to convert between string and enum.</p>



<a name="215912808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/using%20enum%20for%20members%20of%20rustc_target%3A%3Aspec%3A%3ATarget%20%3F/near/215912808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> est31 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/using.20enum.20for.20members.20of.20rustc_target.3A.3Aspec.3A.3ATarget.20.3F.html#215912808">(Nov 06 2020 at 22:38)</a>:</h4>
<p>Yes to make it clear, endian should definitely become an enum.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>